import CocosHelper from "../../TFrame/script/helper/CocosHelper";
import EventCenter from "../../TFrame/script/helper/EventCenter";
import EventNames from "../EventNames";
import CoinSystem from "../system/CoinSystem";
import Util from "../../../StartAb/Util";
import GetCoinFreeDialog from "./GetCoinsFreeDialog";
import ShopDialog from "./ShopDialog";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CoinRoot extends cc.Component {
    @property(cc.SpriteFrame)
    flyCoinFrame: cc.SpriteFrame = null;

    @property(cc.Node)
    coinIconNode: cc.Node = null;

    @property(cc.Node)
    coinTextNode: cc.Node = null;

    @property(cc.AudioClip)
    flyAudio: cc.AudioClip = null;

    start() {
        this.coinTextNode.getComponent(cc.Label).string = CoinSystem.coinInMomory.toString();
        EventCenter.on(EventNames.FlyGetCoinEvent, this.flyGetCoinsAni, this);
        EventCenter.on(EventNames.ConsumeCoins, () => {
            this.coinTextNode.getComponent(cc.Label).string = CoinSystem.coinInMomory.toString();
        }, this)
    }

    flyGetCoinsAni(coin: number, startPosWorld: cc.Vec2 = cc.Vec2.ZERO) {
        let outDelay = 0.05;
        let coinCount = 10;
        for (let i = 0; i < coinCount; ++i) {
            let n = new cc.Node;
            let spr = n.addComponent(cc.Sprite);
            n.parent = this.node;
            spr.spriteFrame = this.flyCoinFrame;
            n.scale = 0.5
            let x = Util.random(-40, 40);
            let y = Util.random(-30, 10);
            n.setPosition(this.node.convertToNodeSpaceAR(startPosWorld.add(cc.v2(x, y))));
            n.opacity = 0;
            cc.audioEngine.playEffect(this.flyAudio, false);
            cc.tween(n)
                .delay(outDelay * i)
                .set({ opacity: 255 })
                .by(0.2, { y: -10 }, { easing: 'backOut' })
                .to(0.5, { position: cc.v3(CocosHelper.getPositionForNewParent(this.coinIconNode, this.node)) })
                .call(() => {
                    if (i == coinCount - 1) {
                        this.flyCoinsDone();
                    }
                    if (i == 0) {
                        this.showCoinAddNumber(coin)
                    }
                })
                .removeSelf()
                .start()
        }
    }

    flyCoinsDone() {
        cc.tween(this.coinTextNode)
            .to(0.2, { scale: 1.5 })
            .call(() => {
                this.coinTextNode.getComponent(cc.Label).string = CoinSystem.coinInMomory.toString();
                EventCenter.emit(EventNames.FlyCoinDoneEvent);
            })
            .to(0.2, { scale: 1.0 })
            .start();
    }

    showCoinAddNumber(num: number) {
        let n = new cc.Node();
        let lb = n.addComponent(cc.Label);
        lb.fontSize = this.coinTextNode.getComponent(cc.Label).fontSize - 4;
        lb.string = "+" + num.toString();
        n.parent = this.coinTextNode.parent;
        n.color = cc.Color.GREEN;
        lb.enableBold = true;
        n.position = this.coinTextNode.position.sub(cc.v3(0, 50));
        cc.tween(n).delay(1.5).removeSelf().start();
    }

    getCoin() {
        // if (cc.sys.os == cc.sys.OS_IOS) {
        // } else {
        //     ShopDialog.show(this.node)
        // }
        // GetCoinFreeDialog.show(this.node)
    }
}
